package csc.daonjpa.java.controller;

import java.util.Date;
import java.util.List;

import javax.persistence.PersistenceException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import csc.daonjpa.java.domain.Account;
import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.service.AccountService;
import csc.daonjpa.java.service.CustomerService;

/**
 * This controller handles account related actions
 * 
 * @author nvu3
 * 
 */
@Controller
public class AccountController {
	private static final Logger LOG = Logger.getLogger(AccountController.class);

	private AccountService bankService;
	private CustomerService customerService;

	public CustomerService getCustomerService() {
		return customerService;
	}

	@Autowired
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public AccountService getBankService() {
		return bankService;
	}

	@Autowired
	public void setBankService(AccountService bankService) {
		this.bankService = bankService;
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/account")
	public String registerAccountPage() {
		return "forward:/listAccount";
	}

	/**
	 * Adds account to DB
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/createAccount")
	public String addAccount(HttpSession session, HttpServletRequest request, Model model) {
		// Read account info from request and save into Account object
		Double availableAmount = Double.parseDouble(request
				.getParameter("availableAmount"));
		String accountType = request.getParameter("accountType");
		String accountName = request.getParameter("accountName");
		String accountNumber = request.getParameter("accountNumber");
		String loginId = session.getAttribute("loginSession").toString();
		Date now = new Date();
		Customer customer = customerService.getCustomer(loginId);
		
		Account account = new Account(availableAmount, accountNumber,
				accountName, accountType, now, now, customer);
		try {
			// Save account to DB
			bankService.addAccount(account);
			model.addAttribute("successMessage",
					"Added new account successfully");
		} catch (PersistenceException ex) {
			if (ex.getCause() instanceof ConstraintViolationException) {
				model.addAttribute("errorMessage", "Duplicated account found");
			} else {
				model.addAttribute("errorMessage", "Error adding new account");
			}
			LOG.error("Error found adding an account", ex);
			throw ex;
		} catch (Exception e) {
			LOG.error("General Error found adding an account", e);
			throw e;
		}

		return "forward:/listAccount";
	}
	/**
	 * show list account of user logged in.
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/listAccount")
	public String getAccountList(HttpSession session, Model model) {
		String loginId = session.getAttribute("loginSession").toString();
		// Get the list of all accounts logged in from DB
		List<Account> accountList = bankService.getAccountByCustomer(loginId);
		model.addAttribute("listAccount", accountList);
		return "accountList";
	}

	@ExceptionHandler(Exception.class)
	public ModelAndView handleAllException(Exception ex) {

		ModelAndView model = new ModelAndView("accountList");
		model.addObject("errorMessage", "Exception occurred");

		return model;
	}
}
